package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.pb.UnionWarActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = CrossProtocol.C_UNION_WAR_PREPARE_DETAIL, desc = "备战详情")
public class CrossUnionWarPrepareDetailCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {


        CrossUnionWarProto.CrossPrepareOverviewReqMsg reqMsg = CrossUnionWarProto.CrossPrepareOverviewReqMsg.parseFrom(packet.getBytes());
        ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(reqMsg.getActivityId());
        String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(reqMsg.getActivityId(), packet.getPlayerId());

        UnionWarActivityServerProto.PrepareDetailRespMsg.Builder respMsg = UnionWarActivityServerProto.PrepareDetailRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setPhase(reqMsg.getPhase());
        respMsg.setSession(reqMsg.getEndSession());
        respMsg.setActivityId(activityId);
        if(StringUtils.isNullOrEmpty(unionUid)){
            respMsg.setRet(GameErrorCode.E_UNION_NO_JOIN_UNION);
        }
        if(activityInfo == null){
            respMsg.setRet(GameErrorCode.E_ACTIVITY_OVER_TIME);
        }
        if(respMsg.getRet() != 0){
            respMsg.setActivityId(activityId);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_DETAIL, respMsg));
            return;
        }


        CrossUnionWarDetail detail = CrossUnionWarMgr.getCrossUnionWarDetailReadOnly(reqMsg.getActivityId(), reqMsg.getPhase(), reqMsg.getEndSession(), unionUid);

        if(detail != null){
            UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(activityId, packet.getPlayerId());
            CrossUnionWarSimple simple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, unionUid);
            UnionBaseInfo unionInfo = null;
            if(simple != null){
                unionInfo = simple.getUnionBaseInfo();
            }
            UnionBaseInfo enemy = null;
            CrossUnionWarSimple enemySimple = CrossUnionWarMgr.getCrossUnionWarSimple(activityId, detail.getEnemyUnionUid());
            if(enemySimple != null) {
                enemy = enemySimple.getUnionBaseInfo();
            }
            UnionWarActivityServerProto.SessionLineupTemp.Builder tempBuilder = UnionWarActivityPb.parseSessionLineupTempList(detail, true, groupUnion.getUserMap().size(), enemy, unionInfo);
            respMsg.setLineupList(tempBuilder);
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_PREPARE_DETAIL, respMsg));
    }

    @Override
    public void sendData() {

    }
}
